package com.lmt.ymcc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.lmt.ymcc.domain.CourseMarket;
import com.lmt.ymcc.domain.CourseUserLearn;
import com.lmt.ymcc.dto.AliPayMessageDto;
import com.lmt.ymcc.mapper.CourseUserLearnMapper;
import com.lmt.ymcc.service.ICourseMarketService;
import com.lmt.ymcc.service.ICourseUserLearnService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AmazingXu
 * @since 2022-08-08
 */
@Service
public class CourseUserLearnServiceImpl extends ServiceImpl<CourseUserLearnMapper, CourseUserLearn> implements ICourseUserLearnService {

    @Autowired
    private ICourseMarketService courseMarketService;

    // 判断用户是否购买此章节视频
    @Override
    public CourseUserLearn checkUserLearn(Long loginId, Long courseId) {
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("login_id",loginId);
        wrapper.eq("course_id", courseId);
        CourseUserLearn courseUserLearn = selectOne(wrapper);
        if (courseUserLearn != null) {
            return courseUserLearn;
        }
        return null;
    }

    // 添加用户购买信息
    @Override
    public void addUserLearn(AliPayMessageDto dto) {
        Map<String,Object> map = JSONObject.parseObject(dto.getExtParams(), Map.class);
        // 查询用户的购买记录
        Long loginId = Long.valueOf(map.get("loginId").toString());
        String courseIdStr = map.get("courseId").toString();
        String[] ids = courseIdStr.split(",");
        Date now = new Date();
        for (String id : ids) {
            Long courseId = Long.valueOf(id);
            CourseUserLearn tmp = selectByLoginIdAndCourseId(loginId, courseId);
            CourseMarket courseMarket = courseMarketService.selectById(courseId);
            if(tmp == null){
                // 新增
                CourseUserLearn learn = new CourseUserLearn();
                learn.setLoginId(loginId);
                learn.setStartTime(now);
                learn.setEndTime(DateUtils.addDays(now,courseMarket.getValidDays()));
                learn.setState(0);
                learn.setCourseId(courseId);
                learn.setCourseOrderNo(dto.getOrderNo());
                learn.setCreateTime(now);
                insert(learn);
            }else{
                // 查询到了，说明用户之前购买了，直接累加时间就可以
                tmp.setCourseOrderNo(dto.getOrderNo());
                Date nowDay = new Date();
                if (tmp.getEndTime().getTime() < nowDay.getTime()) {
                    // 说明用户是很久之前购买了，所以可以观看天数应该从现在购买开始算起
                    tmp.setStartTime(nowDay);
                    tmp.setEndTime(DateUtils.addDays(nowDay,courseMarket.getValidDays()));
                }
                tmp.setEndTime(DateUtils.addDays(tmp.getEndTime(),courseMarket.getValidDays()));
                updateById(tmp);
            }
        }
    }

    // 通过用户ID和课程id查询用户购买信息
    private CourseUserLearn selectByLoginIdAndCourseId(Long loginId, Long courseId) {
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("login_id",loginId);
        wrapper.eq("course_id",courseId);
        return selectOne(wrapper);
    }
}
